package com.basic.modules.ykf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.basic.common.utils.ParamsModel;
import com.basic.common.utils.ParamUtils;
import com.basic.modules.sys.service.SysUserService;
import com.basic.modules.ykf.entity.YkfCompanyDeptEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basic.common.utils.PageUtils;
import com.basic.common.utils.Query;

import com.basic.modules.ykf.dao.YkfCustomGroupDao;
import com.basic.modules.ykf.entity.YkfCustomGroupEntity;
import com.basic.modules.ykf.service.YkfCustomGroupService;

import java.util.List;


@Service("ykfCustomGroupService")
public class YkfCustomGroupServiceImpl extends ServiceImpl<YkfCustomGroupDao, YkfCustomGroupEntity> implements YkfCustomGroupService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public PageUtils queryPage(ParamsModel<YkfCustomGroupEntity> params) {
        if (ObjectUtil.isEmpty(params.getSort()) || StrUtil.isBlank(params.getSort().getSidx()) || StrUtil.isBlank(params.getSort().getOrder())) {
            ParamsModel.SortParams sortParams = new ParamsModel.SortParams();
            sortParams.setSidx("createTime");
            sortParams.setOrder("desc");
            params.setSort(sortParams);
        }
        QueryWrapper<YkfCustomGroupEntity> queryWrapper = ParamUtils.createQueryWrapperByDefaultSort(params);
        List<String> powerCompanyNos = sysUserService.getPowerCompanyNos();
        if (ObjectUtil.isNotEmpty(powerCompanyNos)) {
            if (!powerCompanyNos.contains("ALL")) {
                queryWrapper.lambda().in(YkfCustomGroupEntity::getCompanyNo, powerCompanyNos);
            }
        } else {
            return new PageUtils();
        }
        if (StrUtil.isNotBlank(params.getCondition().getKeyword())) {
            queryWrapper.lambda().like(YkfCustomGroupEntity::getCustomGroupName, params.getCondition().getKeyword());
        }
        IPage<YkfCustomGroupEntity> page = this.page(
                new Query<YkfCustomGroupEntity>().getPage(BeanUtil.beanToMap(params.getPage())),
                queryWrapper
        );
        return new PageUtils(page);
    }

}
